#include "aky_program/aky_com_include.h"

e_mount_type_t g_mount_type = MOUNT_SPI_FLASH;
uint32_t g_u32_tick_cnt = 0;
char fmt_buf[200];
char fmt_buf2[200];
#define BL_PWM_MAX_DUTY (10000ul)//3.7khz

// #define BL_PWM_MAX_DUTY (40000ul)//1khz
void set_back_light_init(void)
{
	PWM_InitStructure  PWM_initStruct;
    uint16_t duty = 0;
 
    
    duty = (g_s_set.bl_level * BL_LEVEL_STEP_VAL) * 100;
    #if (AKY_SWITCH_DEVELOP_BOARD == 1)
	PORT_Init(PORTD, PIN9,  PORTD_PIN9_PWM1B,   0);
	PWM_initStruct.Mode = PWM_CENTER_ALIGNED;
	PWM_initStruct.Clkdiv = 2;					//F_PWM = 20M/4 = 5M
	PWM_initStruct.Period = 10000;				//5M/10000 = 500Hz，中心对称模式下频率降低到250Hz
	PWM_initStruct.HdutyA =  2500;				//2500/10000 = 25%
	PWM_initStruct.DeadzoneA = 50;				//50/5M = 10us
	PWM_initStruct.IdleLevelA = 0;
	PWM_initStruct.IdleLevelAN= 0;
	PWM_initStruct.OutputInvA = 0;
	PWM_initStruct.OutputInvAN= 0;
	PWM_initStruct.HdutyB = duty;				//7500/10000 = 75%
	PWM_initStruct.DeadzoneB = 50;				//50/5M = 10us
	PWM_initStruct.IdleLevelB = 0;
	PWM_initStruct.IdleLevelBN= 0;
	PWM_initStruct.OutputInvB = 0;
	PWM_initStruct.OutputInvBN= 0;
	PWM_initStruct.UpOvfIE    = 0;
	PWM_initStruct.DownOvfIE  = 0;
	PWM_initStruct.UpCmpAIE   = 0;
	PWM_initStruct.DownCmpAIE = 0;
	PWM_initStruct.UpCmpBIE   = 0;
	PWM_initStruct.DownCmpBIE = 0;
	PWM_Init(PWM1, &PWM_initStruct);
	PWM_Start(PWM1_MSK);

    #else
	PORT_Init(PORTN, PIN4,  PORTN_PIN4_PWM2AN,   0);
	PWM_initStruct.Mode = PWM_CENTER_ALIGNED;
	PWM_initStruct.Clkdiv = 2;					//F_PWM = 20M/4 = 5M
	PWM_initStruct.Period = BL_PWM_MAX_DUTY;				//5M/10000 = 500Hz，中心对称模式下频率降低到250Hz
    //(150000000 / 2)/ (10000 / 2)
	PWM_initStruct.HdutyA =  (BL_PWM_MAX_DUTY - duty);				//2500/10000 = 25%
	PWM_initStruct.DeadzoneA = 50;				//50/5M = 10us
	PWM_initStruct.IdleLevelA = 0;
	PWM_initStruct.IdleLevelAN= 0;
	PWM_initStruct.OutputInvA = 0;
	PWM_initStruct.OutputInvAN= 0;
	PWM_initStruct.HdutyB = (BL_PWM_MAX_DUTY - duty);				//7500/10000 = 75%
	PWM_initStruct.DeadzoneB = 50;				//50/5M = 10us
	PWM_initStruct.IdleLevelB = 0;
	PWM_initStruct.IdleLevelBN= 0;
	PWM_initStruct.OutputInvB = 0;
	PWM_initStruct.OutputInvBN= 0;
	PWM_initStruct.UpOvfIE    = 0;
	PWM_initStruct.DownOvfIE  = 0;
	PWM_initStruct.UpCmpAIE   = 0;
	PWM_initStruct.DownCmpAIE = 0;
	PWM_initStruct.UpCmpBIE   = 0;
	PWM_initStruct.DownCmpBIE = 0;
	PWM_Init(PWM2, &PWM_initStruct);
	PWM_Start(PWM2_MSK);

    #endif

    set_bl_by_level(g_s_set.bl_level);
    bat_flush_control(BAT_START);
    bat_flush_control(BAT_DISP);

}

//亮度调节 
//pwm_value 0-100
void set_back_light_pwm(uint16_t pwm_value)
{
	PWM_InitStructure  PWM_initStruct;
    uint16_t duty = 0;
    if (pwm_value > 100)
    {
        pwm_value = 100;
    }
    if (pwm_value == 0)
    {
        pwm_value = 1;
    }
    
    duty = pwm_value * 100;
#if (AKY_SWITCH_DEVELOP_BOARD == 1)
    PWM_SetHDuty(PWM1,PWM_CH_B,duty);

#else
    PWM_SetHDuty(PWM2,PWM_CH_A,(BL_PWM_MAX_DUTY - duty));

#endif
}
uint8_t get_bl_level(uint16_t val)
{
    uint8_t res = 0;
    if (val <= 20)
    {
        res = 1;
    }
    else if (val <= 40)
    {
        res = 2;
    }
    else if (val <= 60)
    {
        res = 3;
    }
    else if (val <= 80)
    {
        res = 4;
    }
    else
    {
        res = 5;
    }
		return res;
}
void set_bl_by_level(uint8_t level)
{
    uint16_t pwm_value = 0;
    if (level > 5)
    {
        level = 5;
    }
    if (level == 0)
    {
        level = 1;
    }
    pwm_value = level * BL_LEVEL_STEP_VAL;
    set_back_light_pwm(pwm_value);
}

uint8_t get_mount_exit(void)
{
    uint8_t res = 0;
    if (tp_dev.status == TP_STATE_PRESSED)
    {
        if (tp_dev.x > 645 && tp_dev.x < 900)
        {
            if (tp_dev.y > 510 && tp_dev.y < 580)
            {
                res = 1;
            }
        }
    }
    return res;
}


void mount_spi_flash_to_usb(void)
{
    uint32_t n=0;
    uint16_t step = 0;
    g_mount_type = MOUNT_SPI_FLASH;
    SYS->USBPHYCR = 0x000001db;
    SYS->USBCR = 0;

    MSC_Init2();
    USBD_Open();

    step = 0;
    while(1==1)
    {
        MSC_ProcessOUT();
        
        if(++n % 1000000 == 0)
        {
            FlashDiskFlush();
        }
        if (get_mount_exit() == 1)
        {
            break;
        }
        if (step == 0)
        {
            if (BulkOutSize != 0 && MSC_State == 0)
            {
                WDT_Init(WDT, 0, (1024 * 0.3));
                WDT_Start(WDT);
                
                step = 1;
            }
            
        }
        else if (step == 1)
        {
        }
    }
    USBD_Close();
    MSC_Init();
	SYS->CLKEN0 &= ~(0x01 << SYS_CLKEN0_USB_Pos); 
	SYS->USBPHYCR = 0x00000042;
	SYS->USBCR = 0;
    g_mount_type = MOUNT_SPI_FLASH;

}



void mount_sd_to_usb(void)
{
    uint32_t n=0;
    g_mount_type = MOUNT_SD_CARD;
    SYS->USBPHYCR = 0x000001db;
    SYS->USBCR = 0;
    MSC_Init2();
    USBD_Open();
    while(1==1)
    {
        MSC_ProcessOUT();
        
        if(++n % 1000000 == 0)
        {
            FlashDiskFlush();
        }
        if (get_mount_exit() == 1)
        {
            break;
        }
    }

    USBD_Close();
    MSC_Init();
	SYS->CLKEN0 &= ~(0x01 << SYS_CLKEN0_USB_Pos); 
	SYS->USBPHYCR = 0x00000042;
	SYS->USBCR = 0;
    g_mount_type = MOUNT_SPI_FLASH;
}




uint16_t bat_bar_val = 0;
uint16_t bat_label_val = 0;
uint8_t flag_bat_flash_disp = true;
uint8_t flag_bat_flash_enable = true;

lv_obj_t * bar_bat;
static lv_style_t style_bat_indic;
static lv_style_t style_bat;
lv_obj_t *img_bat;
static lv_task_t * task_bat;
lv_obj_t * label_bat_percent;
lv_obj_t *img_bat_flash;//闪电图标 代表正在充电


void bat_bar_fast_set_val(uint16_t val)
{
    if (val > 100)
    {
        val = 100;
    }
    lv_bar_set_value(bar_bat, val, LV_ANIM_OFF);
}

void bat_bar_set_anim_target_val(uint16_t bar_val, uint16_t label_val)
{
    if (bar_val > 100)
    {
        bar_val = 100;
    }
    if (label_val > 100)
    {
        label_val = 100;
    }
    
    bat_bar_val = bar_val;
    bat_label_val = label_val;
}

void set_disp_bat_flash(uint8_t flag_onoff)
{
    flag_bat_flash_disp = flag_onoff;
}
void bar_anim(lv_task_t* t)
{
    lv_label_set_text_fmt(label_bat_percent, "%d%%",bat_label_val);
    bat_bar_fast_set_val(bat_bar_val);
    if (flag_bat_flash_enable == true)
    {
        lv_obj_set_hidden(img_bat_flash,flag_bat_flash_disp);
    }
    

}

void bat_bar_init(void)
{
    static uint8_t flag_init_onece = 0;
    if (flag_init_onece == 0)
    {
        flag_init_onece = 1;
        bat_bar_val = 0;
        bat_label_val = 0;
        flag_bat_flash_disp = true;
        flag_bat_flash_enable = true;
    }
    

    img_bat = lv_img_create(lv_scr_act(), NULL);

    lv_img_set_src(img_bat,"S:spi:img/bat_img.bin");
    lv_obj_set_pos(img_bat, 30, 25);

    lv_obj_set_style_local_image_recolor(img_bat, LV_IMG_PART_MAIN,LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_set_style_local_image_recolor_opa(img_bat,LV_IMG_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_COVER);


    img_bat_flash = lv_img_create(lv_scr_act(), NULL);
    lv_img_set_src(img_bat_flash,"S:spi:img/bat_flash.bin");
    lv_obj_align(img_bat_flash,img_bat,LV_ALIGN_OUT_LEFT_TOP,1,0);

    bar_bat = lv_bar_create(img_bat, NULL);


    lv_style_init(&style_bat);
    lv_style_set_radius(&style_bat, LV_STATE_DEFAULT, 0);//这里的参数0意思是进度条的指示器样式弧度为零，也就是直角。
    lv_style_set_bg_color(&style_bat, LV_STATE_DEFAULT, LV_COLOR_WHITE);//进度条背景颜色
    // lv_style_set_border_width(&style_bat, LV_STATE_DEFAULT, 2); //进度条背景线条宽度
    // lv_style_set_border_color(&style_bat, LV_STATE_DEFAULT, LV_COLOR_BLACK);//颜色
    lv_style_set_bg_opa(&style_bat, LV_STATE_DEFAULT, LV_OPA_TRANSP);

    lv_style_set_pad_top(&style_bat, LV_STATE_DEFAULT, 5);//指示器到背景四周的距离
    lv_style_set_pad_bottom(&style_bat, LV_STATE_DEFAULT, 5);
    lv_style_set_pad_left(&style_bat, LV_STATE_DEFAULT, 5);
    lv_style_set_pad_right(&style_bat, LV_STATE_DEFAULT, 10);


    lv_obj_add_style(bar_bat, LV_BAR_PART_BG, &style_bat);//样式加到进度条中
    lv_obj_set_size(bar_bat, 55, 30); //进度条大小
    lv_obj_set_pos(bar_bat, 0, 0);

    label_bat_percent = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_long_mode(label_bat_percent, LV_LABEL_LONG_BREAK);     /*Break the long lines*/
    lv_obj_set_width(label_bat_percent, 80);
    lv_obj_set_height(label_bat_percent, 40);
    lv_obj_align(label_bat_percent,img_bat,LV_ALIGN_OUT_RIGHT_TOP,10,0);
    lv_obj_set_style_local_text_letter_space(label_bat_percent,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,2);
    lv_font_t* font_temp = synwit_ui_font_load("S:spi:wqyMicroHeiMono_28.hmf", false);
    lv_obj_set_style_local_text_font(label_bat_percent,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,font_temp);

    bar_anim(NULL);
    task_bat = lv_task_create(bar_anim, 1000, LV_TASK_PRIO_LOW, NULL);
    lv_style_init(&style_bat_indic);
    lv_style_set_radius(&style_bat_indic, LV_STATE_DEFAULT, 0);//这里的参数0意思是进度条的指示器样式弧度为零，也就是直角。
    lv_style_set_bg_color(&style_bat_indic, LV_STATE_DEFAULT, LV_COLOR_BLACK);//进度条背景颜色
    lv_style_set_bg_opa(&style_bat_indic, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_bar_set_range(bar_bat, 0, 100);  //lv_bar_set_range(bar, min, max)进度条范围
    lv_obj_add_style(bar_bat, LV_BAR_PART_INDIC, &style_bat_indic);//样式加到进度条中

}
void bat_bar_stop(void)
{
    if (img_bat != NULL)
    {
        lv_obj_del(img_bat);
        img_bat = NULL;
    }
    if (bar_bat != NULL)
    {
        lv_obj_del(bar_bat);
        bar_bat = NULL;
    }
    if (label_bat_percent != NULL)
    {
        lv_obj_del(label_bat_percent);
        label_bat_percent = NULL;
    }    
    if (img_bat_flash != NULL)
    {
        lv_obj_del(img_bat_flash);
        img_bat_flash = NULL;
    }    
    if (task_bat != NULL)
    {
        lv_task_del(task_bat);
        task_bat = NULL;
    }
    
}

s_time_t g_s_time = {0};
void get_time(void)
{
    static uint32_t u32_tick_last;
    stru_aky_task_0_t s_aky_task_0_temp = {0};
    if (synwit_ui_get_cur_screen_id() == SCREEN012 || synwit_ui_get_cur_screen_id() == SCREEN015)
    {
        return;
    }
    
    s_aky_task_0_temp = aky_task_0_get_data();
    if (g_u32_tick_cnt - u32_tick_last > 1000 / 10)
    {
        u32_tick_last = g_u32_tick_cnt;
        aky_task_0_send_cmd(TASK0_get_TIME_Command);
    }
    if (s_aky_task_0_temp.flag_time_get_succ == 1)
    {
        clr_get_time_flag();
        g_s_time = s_aky_task_0_temp.s_get_time;
    }

    g_s_time.secon = g_secon;

#if 00 && (AKY_DEBUG_ENABLE == 1)
    static uint16_t secon_last = 0;
    static uint16_t secon_temp = 0;
    static uint8_t flag_init_once = 0;
    if (flag_init_once == 0)
    {
        flag_init_once = 1;
        g_s_time.hour = DEFUALT_HOUR + 1;
        g_s_time.min = DEFUALT_MIN + 1;
    }

    {
        if (secon_last != g_secon)
        {
            secon_last = g_secon;
            secon_temp++;
            if (secon_temp > 59 || secon_temp == 0)
            {
                secon_temp = 0;
                g_s_time.min++;
                if (g_s_time.min > 60 || g_s_time.min == 0)
                {
                    g_s_time.min = 0;
                    g_s_time.hour++;
                    if (g_s_time.hour > 23 || g_s_time.hour == 0)
                    {
                        g_s_time.hour = 0;
                        g_s_time.day++;
                        if (g_s_time.day > 30 || g_s_time.day == 0)
                        {
                            g_s_time.day = 0;
                            g_s_time.month++;
                            if (g_s_time.month > 12 || g_s_time.month == 0)
                            {
                                g_s_time.month = 0;
                                g_s_time.year++;
                                if (g_s_time.year > 99 || g_s_time.year == 0)
                                {
                                    g_s_time.year = 0;
                                }
                            }
                        }
                    }
                    
                }
    
            }
    
        }
        
        
    }


#endif
    
}

uint8_t get_week(s_time_t s_time)
{
    uint8_t week_res = 0;
	int year, month, day;
    year = 2000+s_time.year;
    month = s_time.month;
    day = s_time.day;
	{
		if (month == 1 || month == 2)//判断month是否为1或2　
			year--, month += 12;
		int c = year / 100;
		int y = year - c * 100;
		int week = y + y / 4 + c / 4 - 2 * c + 26 * (month + 1) / 10 + day - 1;
		while (week < 0)
			week += 7;
		week %= 7;
        week_res = week;
		// switch (week)
		// {
    	// 	case 1:printf("1Monday\n"); break;
    	// 	case 2:printf("2Tuesday\n"); break;
    	// 	case 3:printf("3Wednesday\n"); break;
    	// 	case 4:printf("4Thursday\n"); break;
    	// 	case 5:printf("5Friday\n"); break;
    	// 	case 6:printf("6Saturday\n"); break;
    	// 	case 0:printf("0Sunday\n"); break;
		// }
	}
    return week_res;
}
const char *chn_week_idex[7] = {"日","一","二","三","四","五","六"};
static lv_obj_t *label_time_sub;          //
static lv_obj_t *label_time;          //
static uint8_t flag_refresh_disp = 0;

void time_flush_init(void)
{
    label_time = lv_label_create(lv_scr_act(), NULL);
    lv_label_set_long_mode(label_time, LV_LABEL_LONG_BREAK);     /*Break the long lines*/
    lv_obj_set_width(label_time, 500);
    lv_obj_set_height(label_time, 50);
    lv_obj_set_pos(label_time, 465, 25);
    lv_obj_set_style_local_text_letter_space(label_time,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,2);
    lv_font_t* font_temp = synwit_ui_font_load("S:spi:wqyMicroHeiMono_28.hmf", false);
    lv_obj_set_style_local_text_font(label_time,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,font_temp);

        flag_refresh_disp = 1;
        label_time_sub = lv_label_create(label_time, label_time);
        lv_label_set_long_mode(label_time_sub, LV_LABEL_LONG_BREAK);     /*Break the long lines*/
        lv_obj_set_width(label_time_sub, lv_obj_get_width(label_time));
        lv_obj_set_pos(label_time_sub, 286, 0);

}
void time_flush(void)
{
    static lv_obj_t *label_time_last; //
    static uint16_t time_cnt = 0;
    static uint16_t time_cnt2 = 0;
    static uint8_t flag_time_point_stat = 0;
    static uint8_t year;                  //
    static uint8_t month;                 //
    static uint8_t day;                   //
    static uint8_t hour;                  //
    static uint8_t min;                   //
    static uint8_t second_last = 0;
    static uint8_t second;                //
    static uint32_t u32_tick_last = 0;                //
    static uint8_t flag_syn_time_onece = 0;
    static uint8_t min_last = 0;

    if (label_time == NULL)
    {
        return;
    }
    if (flag_syn_time_onece == 0)
    {
        if (g_s_time.month != vermon || g_s_time.day != verday || (g_s_time.hour != DEFUALT_HOUR && g_s_time.min != DEFUALT_MIN))
        {
            flag_syn_time_onece = 1;
        }
        return;
    }
    

    if (second_last != g_s_time.secon || flag_refresh_disp)
    {
        second_last = g_s_time.secon;
        if (flag_time_point_stat == 0)
        {
            flag_time_point_stat = 1;
        }
        else
        {
            flag_time_point_stat = 0;
        }
        if (flag_time_point_stat == 1)
        {
            lv_label_set_text(label_time_sub, ":");
        }
        else
        {
            lv_label_set_text(label_time_sub, " ");
        }

        if (flag_refresh_disp)
        {
            lv_obj_set_hidden(label_time, false);
            lv_obj_set_hidden(label_time_sub, false);

        }
        
        if (min_last != g_s_time.min || flag_refresh_disp)        
        {
            min_last = g_s_time.min;

            year = g_s_time.year;
            month = g_s_time.month;
            day = g_s_time.day;
            hour = g_s_time.hour;
            min = g_s_time.min;
            if (g_flag_emc_mode == 1 && synwit_ui_get_cur_screen_id() == SCREEN008)
            {
                lv_label_set_text_fmt(label_time, "20%02d年%02d月%02d日   %02d %02d 星期%semc", year, month, day, hour, min,chn_week_idex[get_week(g_s_time)]);
            }
            else
            {
                lv_label_set_text_fmt(label_time, "20%02d年%02d月%02d日   %02d %02d 星期%s", year, month, day, hour, min,chn_week_idex[get_week(g_s_time)]);
            }
        }
        flag_refresh_disp = 0;

    }


}

lv_obj_t *img_signal;
void signal_disp_init(void)
{
    img_signal = lv_img_create(lv_scr_act(), NULL);
    
    // lv_img_set_src(img_signal,"S:spi:img/signal_off.bin");
    // lv_obj_set_pos(img_signal, 950, 25);
    if (get_csq_val() == 0 || get_csq_val() == 99)
    {
        signal_set_on_off(0);
    }
    else
    {
        signal_set_on_off(1);
    }

}
void signal_disp_stop(void)
{
    if (img_signal != NULL)
    {
        lv_obj_del(img_signal);
        img_signal = NULL;
    }
}

void signal_set_on_off(uint8_t flag_on_off)
{
    if (flag_on_off == 1)
    {
        lv_img_set_src(img_signal,"S:spi:img/signal_on.bin");
    }
    else
    {
        lv_img_set_src(img_signal,"S:spi:img/signal_off.bin");
    }
    lv_obj_set_pos(img_signal, 950, 25);
}
void signal_flush_pro(void)
{
    static uint32_t u32_tick_last = 0;
    if (g_u32_tick_cnt - u32_tick_last > 3000 / 10)
    {
        u32_tick_last = g_u32_tick_cnt;
        if (get_csq_val() == 0 || get_csq_val() == 99)
        {
            signal_set_on_off(0);
        }
        else
        {
            signal_set_on_off(1);
        }
    }
}

// 阻塞延时
void aky_block_delay_us(const uint32_t us)
{
    volatile uint32_t block_cnt = 0;
    for (uint32_t i = 0; i < us; i++)
    {
        block_cnt = 14;
        while (block_cnt > 0)
        {
            block_cnt--;
            __NOP();
        }
    }
}

// 阻塞延时
void aky_block_delay_ms(const uint32_t ms)
{
    swm_delay_ms(ms);
}

static uint8_t flag_clr_off_time = 0; // 自动关机时间重新计时
void off_time_clr(void)
{
    flag_clr_off_time = 1;
}

static uint8_t all_power_off_flag = 0;
void info_power_off(void)
{
    all_power_off_flag = 1;
}

// 电源控制处理
void all_power_control_pro(void)
{
    int res = 0;
    static uint8_t power_step = 0;
    static uint16_t time_cnt = 0;
    static uint8_t second_last = 0;
#if (DEBUG_NOT_SHUT_DOWN == 1)
return;
#endif
    if (power_step == 0)
    {
        {
            power_step = 1;
        }
        second_last = 0;
    }
    else if (power_step == 1)
    {
        // if (synwit_ui_get_cur_screen_id() != SCREEN006)
        {
            if (second_last != g_s_time.secon)
            {
                second_last = g_s_time.secon;
                time_cnt++;
            }

            if (flag_clr_off_time == 1)
            {
                flag_clr_off_time = 0;

                time_cnt = 0;
            }

            if (time_cnt > 120)
            {
                time_cnt = 0;
                all_power_off_flag = 1;
            }
            if (get_spk_recogni_info() == SPK_SHUTDOWN)
            {
                clr_spk_recogni_info();
                all_power_off_flag = 1;
            }
            
            // if (READ_POWER_KEY_STAT & SINGLE_CLICK)
            // {
            //     READ_POWER_KEY_STAT = BTN_UNPRESS;
            //     // do something when power off
            //     {
            //         if (GET_CHARGE_FLAG == 1)
            //         {
            //             res = synwit_ui_load_screen(SCREEN006); // 到充电界面
            //         }
            //         else
            //         {
            //             all_power_off_flag = 1;
            //         }
            //     }
            // }
        }

        if (all_power_off_flag == 1)
        {
            all_power_off_flag = 0;
            if (get_cur_charge_stat() != NO_CHARGE)
            {
                res = synwit_ui_load_screen(SCREEN013); // 到充电界面
            }
            else
            {
                store_set_to_flash(g_s_set);

                printf("all_power_off\n");
                // if (GET_CHARGE_FLAG == 1)
                // {
                //     res = synwit_ui_load_screen(SCREEN006); // 到充电界面
                // }
                // else
                {
                    set_back_light_pwm(0);//将背光剩余的电放掉 清掉显存 不然下一次启动会有乱的数据
                    aky_block_delay_ms(500);

                    // ALL_POWER_CTRL_OFF;
                }
                my_dac_mute();

                aky_task_0_send_cmd(TASK0_BP_DOWN_Command);
                aky_task_1_send_cmd(TASK1_BP_DOWN_Command);
                aky_block_delay_ms(300);
                my_dac_mute();

                aky_task_0_send_cmd(TASK0_BP_DOWN_Command);
                aky_task_1_send_cmd(TASK1_BP_DOWN_Command);
                aky_block_delay_ms(300);

                aky_task_0_send_cmd(TASK0_BP_DOWN_Command);
                aky_task_1_send_cmd(TASK1_BP_DOWN_Command);

            }
        }
    }
}


s_slide_info_t get_slide_info(lv_point_t p_start, lv_point_t p_end)
{
    s_slide_info_t info_res = {0};
    uint16_t temp;
    if (p_start.x < 400)
    {
        info_res.area = area_left;
    }
    else if (p_start.x > 600)
    {
        info_res.area = area_right;
    }
    else
    {
        info_res.area = area_non;
    }
    
    if (p_end.y < p_start.y)
    {
        info_res.dir = dir_up;
        temp = p_start.y - p_end.y;
        if (temp < 50)
        {
            info_res.strength = strength_non;
            info_res.area = area_non;
        }
        else if (temp < 100)
        {
            info_res.strength = strength_1;
        }
        else if (temp < 200)
        {
            info_res.strength = strength_2;
        }
        else if (temp < 300)
        {
            info_res.strength = strength_3;
        }
        else if (temp < 400)
        {
            info_res.strength = strength_4;
        }
        else
        {
            info_res.strength = strength_5;
        }
    }
    else if (p_end.y > p_start.y)
    {
        info_res.dir = dir_down;
        temp = p_end.y - p_start.y;
        if (temp < 50)
        {
            info_res.strength = strength_non;
            info_res.area = area_non;
        }
        else if (temp < 100)
        {
            info_res.strength = strength_1;
        }
        else if (temp < 200)
        {
            info_res.strength = strength_2;
        }
        else if (temp < 300)
        {
            info_res.strength = strength_3;
        }
        else if (temp < 400)
        {
            info_res.strength = strength_4;
        }
        else
        {
            info_res.strength = strength_5;
        }
    }
    else
    {
        info_res.dir = dir_non;
        info_res.area = area_non;
        info_res.strength = strength_non;
    }
    return info_res;
}

char *get_gender_str(e_gender_t gender)
{
    return (gender == MAN) ? "男" : "女";
}

uint8_t flag_stop_stat_bar_flush = 0;
//状态栏停止刷新  
void stat_bar_stop_flush(uint8_t stat)
{
    flag_stop_stat_bar_flush = 1;
    if (stat == 0)
    {
        if (img_signal != NULL)
        {
            lv_obj_set_hidden(img_signal, true);
        }
        if (bar_bat != NULL)
        {
            lv_obj_set_hidden(bar_bat, true);
        }
        if (label_bat_percent != NULL)
        {
            lv_obj_set_hidden(label_bat_percent, true);
        }
        if (img_bat_flash != NULL)
        {
            lv_obj_set_hidden(img_bat_flash, true);
            flag_bat_flash_enable = 0;
        }
        if (label_time != NULL)
        {
            lv_obj_set_hidden(label_time, true);
        }
        if (label_time_sub != NULL)
        {
            lv_obj_set_hidden(label_time_sub, true);
        }    
        if (img_bat != NULL)
        {
            lv_obj_set_hidden(img_bat, true);
        }
    }
    else if (stat == 1)
    {
        if (img_signal != NULL)
        {
            lv_obj_set_hidden(img_signal, true);
        }
        if (label_time != NULL)
        {
            lv_obj_set_hidden(label_time, true);
        }
        if (label_time_sub != NULL)
        {
            lv_obj_set_hidden(label_time_sub, true);
        }  
    }
    else if (stat == 2)
    {
        flag_stop_stat_bar_flush = 0;
        if (img_bat != NULL)
        {
            lv_obj_set_hidden(img_bat, true);
        }
        if (bar_bat != NULL)
        {
            lv_obj_set_hidden(bar_bat, true);
        }
        if (label_bat_percent != NULL)
        {
            lv_obj_set_hidden(label_bat_percent, true);
            flag_bat_flash_enable = 0;
        }
        if (img_bat_flash != NULL)
        {
            lv_obj_set_hidden(img_bat_flash, true);
        }
        
    }
}
void stat_bar_restart_flush(void)
{
    flag_stop_stat_bar_flush = 0;
    if (img_signal != NULL)
    {
        lv_obj_set_hidden(img_signal, false);
    }
    if (img_bat != NULL)
    {
        lv_obj_set_hidden(img_bat, false);
    }
    if (bar_bat != NULL)
    {
        lv_obj_set_hidden(bar_bat, false);
    }
    if (label_bat_percent != NULL)
    {
        lv_obj_set_hidden(label_bat_percent, false);
    }
    if (img_bat_flash != NULL)
    {
        lv_obj_set_hidden(img_bat_flash, flag_bat_flash_disp);
    }
    flag_bat_flash_enable = 1;

    if (label_time != NULL)
    {
        lv_obj_set_hidden(label_time, false);
    }
    if (label_time_sub != NULL)
    {
        lv_obj_set_hidden(label_time_sub, false);
    }
}
#define GET_LOW_BAT_THRESHOLD 540
#define LOW_BAT_THRESHOLD_OFFSET 15 //没打开屏幕 此时获取电压会偏高
//极端低电压检测, 当小于一定电压时不能开机
void extreme_low_bat_detect(void)
{
    volatile  uint16_t bat_val = 0; //
    uint16_t fil_cnt = 0; //
    uint16_t fil_cnt1 = 0; //
    uint16_t fil_cnt2 = 0; //
    uint16_t time_out = 0; //
    uint16_t u16_temp = 0; //
    uint16_t time_out2 = 0; //
    static wav_obj_t* wav_1 = NULL;

#if (AKY_SWITCH_DEVELOP_BOARD == 1)
return;
#endif
    check_low_bat_init();

    while (1)
    {
        aky_block_delay_ms(1);
		// u16_temp = get_cur_bat_val_by_math();
		// bat_val = ADCgetavg2(ADCgetavg1(u16_temp)); // 读出ADC2平均值算法 后的稳定值
        
        if (check_low_bat_trig(GET_LOW_BAT_THRESHOLD + LOW_BAT_THRESHOLD_OFFSET))
        {
            fil_cnt++;
            fil_cnt1 = 0;
            if (fil_cnt > CHECK_LIMIT * 2)
            {
                fil_cnt = 0;
                time_out2 = 0;
                while (1)
                {
                    aky_block_delay_ms(1);
                    time_out2++;
                    if (time_out2 > 4000)
                    {
                        break;
                    }
                    if (get_cur_bat_val_by_math() > (GET_LOW_BAT_THRESHOLD - (LOW_BAT_THRESHOLD_OFFSET)))
                    {
                        fil_cnt2++;
                        if (fil_cnt2 > CHECK_LIMIT)
                        {
                            fil_cnt2 = 0;
                            if (get_cur_charge_stat() != NO_CHARGE)
                            {
                                goto EXIT;
                            }
                        }
                    }
                    else
                    {
                        time_out2 = 0;
                        fil_cnt2 = 0;
                    }
                }
                
                wav_1 = synwit_wav_open("S:spi:voice/VIOCE_LOW_BAT.wav");
                synwit_wav_set_volume(wav_1, 100,WAV_VOLUME_ADJ_MODE_ABSOLUTE);
                my_dac_open();
                dac_init(DAC_FORMAT_MSB12B);

                synwit_wav_play_sync(wav_1);
                aky_block_delay_ms(300);

                printf("extreme_low_bat trig power_off bat val = %d\n",bat_val);

                aky_task_0_send_cmd(TASK0_BP_DOWN_Command);
                aky_task_1_send_cmd(TASK1_BP_DOWN_Command);
                aky_block_delay_ms(300);
                aky_task_0_send_cmd(TASK0_BP_DOWN_Command);
                aky_task_1_send_cmd(TASK1_BP_DOWN_Command);
                aky_block_delay_ms(300);
                aky_task_0_send_cmd(TASK0_BP_DOWN_Command);
                aky_task_1_send_cmd(TASK1_BP_DOWN_Command);
                set_back_light_pwm(0);//将背光剩余的电放掉 清掉显存 不然下一次启动会有乱的数据

            }
        }
        else
        {
            fil_cnt = 0;
            fil_cnt1++;
            if (fil_cnt1 > CHECK_LIMIT * 4)
            {
                fil_cnt1 = 0;
                break;
            }
        }
    }
EXIT:
    AKY_ASM_NOP();
}

const uint8_t who_level_sys_table[] = {90, 120, 130, 140, 160, 180, 0xff}; // axd
const uint8_t who_level_dia_table[] = {60, 80, 85, 90, 100, 110, 0xff};
// 根据sys和dia确定要显示的who等级
// return   显示的格数
uint8_t get_bar_level_by_sys_dia(uint8_t sys, uint8_t dia)
{
    volatile uint8_t res = 0;
    volatile uint8_t i;
    volatile uint8_t bar_val = 0;
    volatile uint8_t bar_val_last = 0;
    volatile uint8_t who_level_sys_table_size = 0;
    volatile uint8_t who_level_dia_table_size = 0;
    who_level_sys_table_size = sizeof(who_level_sys_table) / sizeof(who_level_sys_table[0]);
    who_level_dia_table_size = sizeof(who_level_dia_table) / sizeof(who_level_dia_table[0]);
    for (i = 0; i < who_level_sys_table_size; i++)
    {
        if (sys < who_level_sys_table[i] || i == (who_level_sys_table_size - 1)) //遍历到最大值也算入
        {
            bar_val += i;
            break;
        }
    }
    bar_val_last = bar_val;
    bar_val = 0;
    for (i = 0; i < who_level_dia_table_size; i++)
    {
        if (dia < who_level_dia_table[i] || i == (who_level_dia_table_size - 1))
        {
            bar_val += i;
            if (bar_val_last > bar_val) // 以最大的一次为准
            {
                bar_val = bar_val_last;
            }
            break;
        }
    }
    res = bar_val;
    return res;
}
int calculate_days_between_dates(int year1, int month1, int day1, int year2, int month2, int day2) {
    struct tm date1 = {0};
    struct tm date2 = {0};

    // 设置第一个日期
    date1.tm_year = year1 - 1900; // 年份从1900开始计算
    date1.tm_mon = month1 - 1;    // 月份从0开始计算
    date1.tm_mday = day1;

    // 设置第二个日期
    date2.tm_year = year2 - 1900;
    date2.tm_mon = month2 - 1;
    date2.tm_mday = day2;

    // 将tm结构转换为时间戳
    time_t time1 = mktime(&date1);
    time_t time2 = mktime(&date2);

    // 计算两个时间戳之间的差值（以秒为单位）
    double difference = difftime(time2, time1);

    // 将秒差值转换为天数差值
    int days_difference = difference / (60 * 60 * 24);

    return days_difference;
}
uint16_t get_last_mem_gap_day(e_user_t e_user,s_mem_t s_mem)
{
    uint16_t res = 0;
    s_mem_t s_mem_temp;
    s_mem_index_t s_mem_index_temp;
    s_mem_index_temp = flash_get_mem_index_data(e_user,&g_e_flash_err,MAIN_ADDR);
    if (s_mem_index_temp.cur_mem_index == 0 || g_e_flash_err == AKY_FLASH_ERR)
    {
        // 记忆空
        return 0;
    }
    s_mem_temp = flash_get_mem_data(e_user,s_mem_index_temp.cur_mem_index,&g_e_flash_err,MAIN_ADDR);
    int year1 = 2023, month1 = 10, day1 = 1;
    int year2 = 2023, month2 = 10, day2 = 15;

    year1 = s_mem_temp.s_time.year + 2000;
    month1 = s_mem_temp.s_time.month;
    day1 = s_mem_temp.s_time.day;

    year2 = s_mem.s_time.year + 2000;
    month2 = s_mem.s_time.month;
    day2 = s_mem.s_time.day;

    int days = calculate_days_between_dates(year1, month1, day1, year2, month2, day2);
    days = abs(days);
    printf("Days between %d-%02d-%02d and %d-%02d-%02d: %d\n", year1, month1, day1, year2, month2, day2, days);

    res = days;
    return res;
}
volatile s_mem_t s_mem_temp;
volatile s_mem_t s_mem_temp_last;
volatile s_mem_index_t s_mem_index_temp;


//您血压偏高，请尽快就医（测量标准：连续5天一直偏高）。
uint8_t get_mem_5_days_high(e_user_t e_user)
{
    volatile uint8_t res = 0;
    volatile uint16_t cur_mem_index_last = 0;
    volatile uint8_t days_high = 0;
    volatile uint8_t flag_tail_mem = 0;
    volatile uint16_t cur_mem_index_last_2 = 0;
    uint16_t max_index = 0;


    s_mem_index_temp = flash_get_mem_index_data(e_user,&g_e_flash_err,MAIN_ADDR);
    if (s_mem_index_temp.cur_mem_index == 0 || g_e_flash_err == AKY_FLASH_ERR)
    {
        // 记忆空
        return 0;
    }
    if (s_mem_index_temp.cur_mem_index < 5 && s_mem_index_temp.flag_mem_overwrite == 0)
    {
        // 记忆小于5
        return 0;
    }
    cur_mem_index_last = s_mem_index_temp.cur_mem_index;
    max_index = 0;
    while (1)
    {
        s_mem_temp_last = flash_get_mem_data(e_user,s_mem_index_temp.cur_mem_index,&g_e_flash_err,MAIN_ADDR);
        cur_mem_index_last_2 = s_mem_index_temp.cur_mem_index;

        s_mem_index_temp.cur_mem_index--;
        if(s_mem_index_temp.flag_mem_overwrite == 1)
        {
            if (s_mem_index_temp.cur_mem_index == 0)
            {
                s_mem_index_temp.cur_mem_index = s_mem_index_temp.mem_set;
            }
            else if (s_mem_index_temp.cur_mem_index == cur_mem_index_last)
            {
                flag_tail_mem = 1;                
            }     
        }
        else
        {
            if (s_mem_index_temp.cur_mem_index == 0)
            {
                flag_tail_mem = 1;
            }            
        }
        if (flag_tail_mem == 1)
        {
            s_mem_temp = flash_get_mem_data(e_user,cur_mem_index_last_2 + 1,&g_e_flash_err,MAIN_ADDR);
        }
        else
        {
            s_mem_temp = flash_get_mem_data(e_user,s_mem_index_temp.cur_mem_index,&g_e_flash_err,MAIN_ADDR);
        }
    int year1 = 2023, month1 = 10, day1 = 1;
    int year2 = 2023, month2 = 10, day2 = 15;

    year1 = s_mem_temp_last.s_time.year + 2000;
    month1 = s_mem_temp_last.s_time.month;
    day1 = s_mem_temp_last.s_time.day;

    year2 = s_mem_temp.s_time.year + 2000;
    month2 = s_mem_temp.s_time.month;
    day2 = s_mem_temp.s_time.day;

    int days = calculate_days_between_dates(year1, month1, day1, year2, month2, day2);
    days = abs(days);
    printf("Days between %d-%02d-%02d and %d-%02d-%02d: %d\n", year1, month1, day1, year2, month2, day2, days);

        if (days == 0)
        {
            /* code */
        }
        else if (days == 1)
        {
            uint8_t who_level_left,who_level_right,who_level_left_2,who_level_right_2;
            who_level_left = get_bar_level_by_sys_dia(s_mem_temp_last.s_left_bp.sys, s_mem_temp_last.s_left_bp.dia);
            who_level_right = get_bar_level_by_sys_dia(s_mem_temp_last.s_right_bp.sys, s_mem_temp_last.s_right_bp.dia);
            who_level_left_2 = get_bar_level_by_sys_dia(s_mem_temp.s_left_bp.sys, s_mem_temp.s_left_bp.dia);
            who_level_right_2 = get_bar_level_by_sys_dia(s_mem_temp.s_right_bp.sys, s_mem_temp.s_right_bp.dia);
            if ((who_level_left > 3 || who_level_right > 3) && (who_level_left_2 > 3 || who_level_right_2 > 3))
            {
                days_high++;
                if (days_high >= 5)
                {
                    break;
                }
            }
            
        }
        else
        {
            //不连续
            break;
        }
        if (flag_tail_mem == 1)
        {
            break;
        }
        max_index++;
        if (max_index >= MAX_MEM_SIZE)
        {
            break;
        }
    }
    if (days_high >= 5)
    {

        res = 1;
    }
    printf("days_high = %d\n",days_high);

    return res;
}



// void fatsf_txt_load_mem(void)
// {

//     lv_fs_file_t file;

//     lv_fs_res_t fs_res;
//     e_user_t e_user_temp = USER_1;
//     char *txt_buf;
//     char fmt_buf[200];
//     char *p_gender_str;
//     uint32_t len = 0;
//     uint32_t real_len = 0;
//     uint16_t row_index = 0;
//     uint16_t chart_index = 0;

//     uint16_t cur_mem_index_last = 0;


//     fs_res = lv_fs_open(&file, "S:sd:TestData.txt", LV_FS_MODE_WR | LV_FS_MODE_RD);
//     if (fs_res == LV_FS_RES_OK)
//     {
//         // 待写入的hex数组数据
//         // char data[] = "姓名:张三 性别:男 年龄:33\n       日期          时间      右侧血压    左侧血压   心率   训练时长\n2022-01-01  17:10:10     120/80     110/70      80      45分钟";
//         txt_buf = (char *)flash_buf;
//         sprintf(txt_buf," ");

//         for (uint8_t i = USER_1; i < USER_MAX; i++)
//         {
//             e_user_temp = i;
//             g_s_mem_index = flash_get_mem_index_data(e_user_temp,&g_e_flash_err,MAIN_ADDR);
//             if (g_s_mem_index.flag_is_user_info_valid == 1)
//             {
//                 if (g_s_mem_index.gender == MAN)
//                 {
//                     p_gender_str = "男";
//                 }
//                 else
//                 {
//                     p_gender_str = "女";
//                 }

//                 sprintf(fmt_buf,"\nUSER%d\n姓名:%s 性别:%s 年龄:%s 电话:%s\n       日期          时间      右侧血压    左侧血压     心率      训练时长",i,g_s_mem_index.user_name_buf,p_gender_str,g_s_mem_index.age_buf,g_s_mem_index.phone_num_buf);
//                 strcat(txt_buf,fmt_buf);

//                 cur_mem_index_last = g_s_mem_index.cur_mem_index;
//                 row_index = 0;
//                 while (1) 
//                 {
//                     if (g_s_mem_index.cur_mem_index == 0 || g_e_flash_err == AKY_FLASH_ERR)
//                     {
//                         // 记忆空
//                         sprintf(fmt_buf,"\nno  mem");
//                         strcat(txt_buf,fmt_buf);

//                         break;
//                     }
                    
//                     g_s_mem = flash_get_mem_data(e_user_temp,g_s_mem_index.cur_mem_index,&g_e_flash_err,MAIN_ADDR);
//                     {
//                         sprintf(fmt_buf,"\n20%d-%02d-%02d  %d:%02d:%02d     %d/%d     %d/%d      %d      %d分钟%d秒",g_s_mem.s_time.year,g_s_mem.s_time.month,g_s_mem.s_time.day,g_s_mem.s_time.hour,g_s_mem.s_time.min,g_s_mem.s_time.secon,g_s_mem.s_right_bp.sys,g_s_mem.s_right_bp.dia,g_s_mem.s_left_bp.sys,g_s_mem.s_left_bp.dia,g_s_mem.s_left_bp.pul,g_s_mem.s_train_time.min,g_s_mem.s_train_time.secon);
//                         strcat(txt_buf,fmt_buf);

//                     }

//                     g_s_mem_index.cur_mem_index--;
//                     if(g_s_mem_index.flag_mem_overwrite == 1)
//                     {
//                         if (g_s_mem_index.cur_mem_index == 0)
//                         {
//                             g_s_mem_index.cur_mem_index = g_s_mem_index.mem_set;
//                         }
//                         else if (g_s_mem_index.cur_mem_index == cur_mem_index_last)
//                         {
//                             break;                
//                         }     
//                     }
//                     else
//                     {
//                         if (g_s_mem_index.cur_mem_index == 0)
//                         {
//                             break;
//                         }            
//                     }


//                     row_index++;
//                     if (row_index >= MAX_MEM_SIZE)
//                     {
//                         break;
//                     }
//                 }
//             }
//         }
        
        


//         // sprintf(fmt_buf,"\n20%d-%02d-%02d  %d:%d:%02d     %d/%d     %d/%d      %d      %d分钟%d秒",22,01,01,17,10,33,120,80,155,111,66,44,22);
//         // strcat(txt_buf,fmt_buf);
//      // 获取文件大小
//         uint32_t file_size;
//         fs_res = lv_fs_size(&file, &file_size);
//         if (fs_res == LV_FS_RES_OK) 
//         {
//             // 分配内存来存储文件内容
//             printf("file_size = %d\n",file_size);
//             char *buffer = (char *)lv_mem_alloc(file_size + 1);
//             if (buffer != NULL) 
//             {
//                 // 读取文件内容
//                 uint32_t bytes_read;
//                 fs_res = lv_fs_read(&file, buffer, file_size, &bytes_read);
//                 if (fs_res == LV_FS_RES_OK) 
//                 {
//                     printf("bytes_read = %d\n",bytes_read);
//                     if (strcmp(txt_buf,buffer) == 0)
//                     {
//                         printf("strcmp(txt_buf,buffer) is equal  return\n");
//                         lv_mem_free(buffer);
//                         lv_fs_close(&file);
//                         return;
//                     }
//                     else
//                     {
//                         printf("strcmp(txt_buf,buffer) not equal wirte data\n");
//                     }
//                 }
//                 else
//                 {
//                     printf("lv_fs_read failed with error code: %d\n", fs_res);
//                 }
//                 lv_mem_free(buffer);
//             }
//         }
        
//         fs_res = lv_fs_seek(&file, 0, LV_FS_SEEK_SET);
//         if (fs_res != LV_FS_RES_OK) {
//             printf("Failed to reset file pointer with error code: %d\n", fs_res);
//             lv_fs_close(&file);
//             return;
//         }

//         fs_res = lv_fs_trunc(&file);
//         if (fs_res != LV_FS_RES_OK)
//         {
//             printf("lv_fs_trunc failed with error code: %d\n", fs_res);
//             lv_fs_close(&file);
//             return;

//         }
//         len = (uint32_t)strlen(txt_buf);
//         printf("write len = %d\n",len);

//         // 写入数据
//         fs_res = lv_fs_write(&file, txt_buf, len, &real_len);
//         if (fs_res != LV_FS_RES_OK) {
//             printf("Failed to write to file.\n");
//         }
//         printf("real_len = %d\n",real_len);
//         lv_fs_close(&file);
//     }
//     else
//     {
//         printf("lv_fs_open failed with error code: %d\n", fs_res);
//         return;
//     }


// }

typedef enum
{
    SYN_IDLE = 0,
    SYN_SEND_MEM,
    SYN_END,
    SYN_UNDEF,
}e_syn_mem_stat_t;
e_syn_mem_stat_t e_syn_mem_stat = SYN_IDLE;
void syn_mem_to_net_control(uint8_t stat)
{
#if (DEBUG_NO_4G_MEM == 1)
    return;
#endif
    if (g_s_set.flag_net_work == 0)
    {
        return;
    }
    
    if (stat == 1)
    {
        if (e_syn_mem_stat == SYN_IDLE || e_syn_mem_stat == SYN_END)
        {
            e_syn_mem_stat = SYN_SEND_MEM;
        }
        else
        {
            printf("mem syn is busy\n");
        }
    }
    
}
static uint8_t flag_send_err = 0;

void syn_mem_to_net_pro(void)
{
    static e_syn_mem_stat_t e_syn_mem_stat_last = SYN_UNDEF;
    static uint16_t step = 0;
    static uint16_t row_index = 0;
    static uint16_t cur_mem_index_last = 0;
    static uint32_t u32_tick_last = 0;
    stru_aky_task_2_t s_task_2_get;
    static uint16_t retry_cnt = 0;
    static uint16_t time_out = 0;
    static s_mem_index_t s_mem_index_temp;
    static s_mem_t s_mem_temp;
    static s_mem_t s_mem_temp_new;
    static uint8_t flag_send_mem = 0;
    static e_user_t e_user_temp = USER_1;


    if (e_syn_mem_stat != e_syn_mem_stat_last)
    {
        e_syn_mem_stat_last = e_syn_mem_stat;
        step = 0;
        u32_tick_last = g_u32_tick_cnt;
        retry_cnt = 0;
        time_out = 0;
    }
    s_task_2_get = aky_task_2_get_data();
    switch (e_syn_mem_stat)
    {
    case SYN_IDLE:
    {
        if (step == 0)
        {
            step++;

        }
        else if (step == 1)
        {
            /* code */
        }
        break;
    }
    case SYN_SEND_MEM:
    {
        if (step == 0)
        {
            step++;
            e_user_temp = g_e_user;
            s_mem_index_temp = flash_get_mem_index_data(e_user_temp,&g_e_flash_err,MAIN_ADDR);
            cur_mem_index_last = s_mem_index_temp.cur_mem_index;
            if (s_mem_index_temp.cur_mem_index == 0 || g_e_flash_err == AKY_FLASH_ERR)
            {
                // 记忆空
                e_syn_mem_stat = SYN_END;
                printf("mem empty \n");
                break;
            }
            row_index = 0;
            retry_cnt = 0;
            time_out = 0;
            flag_send_mem = 0;
            flag_send_err = 0;
            u32_tick_last = g_u32_tick_cnt;
        }
        else if (step == 1)
        {
            step++;
            retry_cnt = 0;
            time_out = 0;
            s_mem_temp = flash_get_mem_data(e_user_temp,s_mem_index_temp.cur_mem_index,&g_e_flash_err,MAIN_ADDR);

            {
                if (s_mem_temp.flag_syn == 0)
                {
                    g_s_mem_send = s_mem_temp;
                    if (flag_send_mem == 1)
                    {
                        aky_task_2_send_cmd(TASK2_SEND_MEM);
                    }
                    else
                    {
                        aky_task_2_send_cmd(TASK2_SEND_RESULT);
                    }
                }
            }
        }
        else if (step == 2)
        {
            if (get_voice_busy_stat() == 1)
            {
                u32_tick_last = g_u32_tick_cnt;
            }
            
            if (g_u32_tick_cnt - u32_tick_last >= 5000 / 10)
            {
                // u32_tick_last = g_u32_tick_cnt;
                if (s_task_2_get.e_aky_task_2_stat == TASK_2_get_succes)
                {
                    //成功 查询发送下一组
                    s_mem_temp_new = flash_get_mem_data(e_user_temp,s_mem_index_temp.cur_mem_index,&g_e_flash_err,MAIN_ADDR);
                    if (memcmp(&s_mem_temp_new,&s_mem_temp,sizeof(s_mem_t)) == 0)
                    {
                        //与上次相同 没有发生覆写
                        s_mem_temp.flag_syn = 1;
                        flash_save_mem_data(s_mem_temp, e_user_temp, s_mem_index_temp.cur_mem_index,MAIN_ADDR);//记忆写入flash
                        printf("memcmp(&s_mem_temp_new,&s_mem_temp,sizeof(s_mem_t)) == 0\n");

                    }
                    else
                    {
                        //与上次不相同 发生了覆写  停止上发
                        e_syn_mem_stat = SYN_END;
                        printf("memcmp(&s_mem_temp_new,&s_mem_temp,sizeof(s_mem_t)) != 0  SYN_END\n");
                        break;
                    }

                    s_mem_index_temp.cur_mem_index--;
                    if(s_mem_index_temp.flag_mem_overwrite == 1)
                    {
                        if (s_mem_index_temp.cur_mem_index == 0)
                        {
                            s_mem_index_temp.cur_mem_index = s_mem_index_temp.mem_set;
                        }
                        else if (s_mem_index_temp.cur_mem_index == cur_mem_index_last)
                        {
                            e_syn_mem_stat = SYN_END;
                        }     
                    }
                    else
                    {
                        if (s_mem_index_temp.cur_mem_index == 0)
                        {
                            e_syn_mem_stat = SYN_END;
                        }            
                    }
                    row_index++;
                    if (row_index >= MAX_MEM_SIZE)
                    {
                        e_syn_mem_stat = SYN_END;
                    }
                    
                    if(e_syn_mem_stat == SYN_SEND_MEM)
                    {
                        printf("send next mem cur_mem_index = %d \n",s_mem_index_temp.cur_mem_index);
                        flag_send_mem = 1;
                        step = 1;
                    }
                    else
                    {
                        printf("SYN_END row_index = %d \n",row_index);
                    }
                }
                else if (s_task_2_get.e_aky_task_2_stat == TASK_2_get_fail)
                {
                    //重发
                    g_s_mem_send = s_mem_temp;
                    aky_task_2_send_cmd(TASK2_SEND_RESULT);
                    printf("retry\n");

                    retry_cnt++;
                    if (retry_cnt > 3)
                    {
                        retry_cnt = 0;
                        e_syn_mem_stat = SYN_END;
                        printf("retry_cnt > 3 SYN_END\n");
                        flag_send_err = 1;
                    }
                    
                }
                else
                {
                    //超时
                    time_out++;
                    if (time_out > 30000 / 10)
                    {
                        time_out = 0;
                        e_syn_mem_stat = SYN_END;
                        printf("time_out  SYN_END\n");
                        flag_send_err = 1;
                    }
                }
            }
            
        }
        
        break;
    }
    case SYN_END:
    {
        if (step == 0)
        {
            step++;
            printf("SYN_END\n");
            if (flag_send_err == 1)
            {
                /* code */
            }
            
        }
        else if (step == 1)
        {
            /* code */
        }
        
        break;
    }
    default:
       break;
    }

}
uint8_t get_send_end_stat(void)
{
    uint8_t res = 0;
    if (e_syn_mem_stat == SYN_END)
    {
        if (flag_send_err == 0)
        {
            res = 1;
        }
        else
        {
            res = 2;
        }
    }

    return res;
}
void screen_flush_pro(void)
{
    if (flag_stop_stat_bar_flush == 0)
    {
        time_flush();
        signal_flush_pro();
    }
    bat_flush_pro();

    all_power_control_pro();
}

void screen_init(void)
{
    bat_bar_init();
    signal_disp_init();
    time_flush_init();
    time_flush();
    off_time_clr();
}
void screen_stop(void)
{
    bat_bar_stop();
    signal_disp_stop();
    if (label_time_sub != NULL)
    {
        lv_obj_del(label_time_sub);
        label_time_sub = NULL;
    }
    if (label_time != NULL)
    {
        lv_obj_del(label_time);
        label_time = NULL;
    }
}

void aky_func_init(void)
{
    static uint8_t flag_init_time = 0;
    if (flag_init_time == 0)
    {
        flag_init_time = 1;
        g_s_time.year = 25;
        g_s_time.month = vermon;
        g_s_time.day = verday;
        g_s_time.hour = DEFUALT_HOUR;
        g_s_time.min = DEFUALT_MIN;
    }


    aky_flash_init();
   aky_task_0_init();
   aky_task_1_init();
   aky_task_2_init();
    aky_task_3_init();
    voice_init();
    bat_init();


    TIMR_Init(BTIMR3, TIMR_MODE_TIMER, CyclesPerUs, 10000, 1); // 10ms中断一次
    TIMR_Start(BTIMR3);

    aky_block_delay_ms(100);

    extreme_low_bat_detect();

    // flash_test_2();
}


uint8_t g_secon = 0;
void BTIMR3_Handler(void)
{
    static uint32_t u32_tick_last_2;
    static uint8_t min_last;
    TIMR_INTClr(BTIMR3,TIMR_IT_TO);

    //10MS
#if 00

#else
charge_stat_detect_pro();
#endif

    // LED_TEST_PIN_FLIP();
    g_u32_tick_cnt++;
    if (g_u32_tick_cnt - u32_tick_last_2 >= 1000 / 10)
    {
        u32_tick_last_2 = g_u32_tick_cnt;

        g_secon++;
        if (g_secon > 59 || g_secon == 0)
        {
            g_secon = 0;
        }
        if (min_last != g_s_time.min)
        {
            min_last = g_s_time.min;
            g_secon = 0;
        }
    }
    

}

